#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <signal.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <setjmp.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>

#include <rte_common.h>
#include <rte_log.h>
#include <rte_malloc.h>
#include <rte_memory.h>
#include <rte_memcpy.h>
#include <rte_eal.h>
#include <rte_launch.h>
#include <rte_cycles.h>
#include <rte_prefetch.h>
#include <rte_lcore.h>
#include <rte_per_lcore.h>
#include <rte_branch_prediction.h>
#include <rte_interrupts.h>
#include <rte_random.h>
#include <rte_debug.h>
#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_string_fns.h>

#define RX_RING_SIZE 1024
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32

static volatile bool force_quit;

static void signal_handler(int signum) {
	if (signum == SIGINT || signum == SIGTERM)
		force_quit = true;
}

int main(int argc, char *argv[]) {
	int ret;
	uint16_t portid;
	struct rte_mempool *mbuf_pool;

	static struct rte_eth_conf port_conf = {
		.rxmode = {
			// .mq_mode = RTE_ETH_MQ_RX_RSS,
		},
		// .rx_adv_conf = {
		// 	.rss_conf = {
		// 		.rss_hf = RTE_ETH_RSS_IP |
		// 		RTE_ETH_RSS_TCP |
		// 		RTE_ETH_RSS_UDP,
		// 	}
		// }
	};
	// port_conf.rxmode.max_rx_pkt_len = RTE_ETHER_MAX_LEN;
	ret = rte_eal_init(argc, argv);
	if (ret < 0)
		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
	argc -= ret;
	argv += ret;

	force_quit = false;
	signal(SIGINT, signal_handler);
	signal(SIGTERM, signal_handler);

	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (mbuf_pool == NULL)
		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");

	portid = rte_eth_find_next_owned_by(0, RTE_ETH_DEV_NO_OWNER);
	if (portid == RTE_MAX_ETHPORTS)
		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");

	printf("Initializing port %u\n", portid);

	if (!rte_eth_dev_is_valid_port(portid)) {
		printf("invalid port \n");
		return -1;

	}

	ret = rte_eth_dev_configure(portid, 1, 0, &port_conf);
	if (ret < 0)
		rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n", ret, portid);

	ret = rte_eth_rx_queue_setup(portid, 0, RX_RING_SIZE, rte_eth_dev_socket_id(portid), NULL, mbuf_pool);
	if (ret < 0)
		rte_exit(EXIT_FAILURE, "Cannot setup RX queue: err=%d, port=%u\n", ret, portid);

	ret = rte_eth_dev_start(portid);
	if (ret < 0)
		rte_exit(EXIT_FAILURE, "Cannot start device: err=%d, port=%u\n", ret, portid);

	printf("Entering main loop\n");

	while (!force_quit) {
		struct rte_mbuf *pkts_burst[BURST_SIZE];
		int nb_rx;

		nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, BURST_SIZE);
		for (int i = 0; i < nb_rx; i++) {
			printf("Received packet of length %u\n", rte_pktmbuf_pkt_len(pkts_burst[i]));
			rte_pktmbuf_free(pkts_burst[i]);
		}
	}

	printf("Closing port %d\n", portid);
	rte_eth_dev_stop(portid);
	rte_eth_dev_close(portid);

	printf("Bye...\n");

	return 0;
}

